#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time: 2018/1/23 下午5:11
@Author: CZC
@File: parse_annual_report_worker.py
"""
from base.parse_base_worker import ParseBaseWorker
from common import util
from common.annual_field import AnnualReports


class ParseAnnualReportWorker(ParseBaseWorker):
    def __init__(self, **kwargs):
        ParseBaseWorker.__init__(self, **kwargs)

    # 年报基本信息
    def get_annual_report_base_info(self, base_info):
        base_info_dict = {}

        pages_list = self.get_crawl_page(base_info, True)
        if not pages_list:
            return base_info_dict
        for page in pages_list:
            json_data = util.json_loads(page.get('text'))
            if not json_data:
                continue
            data_list = json_data.get('data')
            if not data_list:
                continue

            for data in data_list:
                sub_model = {
                    AnnualReports.CODE: data.get('uniscId'),
                    AnnualReports.COMPANY_NAME: data.get('entName'),
                    AnnualReports.ADDRESS: data.get('addr'),
                    AnnualReports.ZIP_CODE: data.get('postalCode'),
                    AnnualReports.CONTACT_NUMBER: data.get('tel'),
                    AnnualReports.EMAIL: data.get('email'),
                    AnnualReports.EMPLOYED_POPULATION: '{}人'.format(data.get('empNum')) if data.get('empNumDis') == '1' else '企业选择不公示',
                    AnnualReports.EMPLOYED_POPULATION_WOMAN: '{}人'.format(data.get('womemPNum')) if data.get('womemPNumDis') == '1' else '企业选择不公示',
                    AnnualReports.BUSINESS_STATUS: data.get('busSt_CN'),
                    AnnualReports.ENTERPRISE_HOLDING: data.get('holdingSmsg_cn') if data.get('holdingSmsgDis') == '1' else '企业选择不公示',
                    AnnualReports.IS_INVEST: '否',  # 默认为否，若具体有数据，变更为是
                    AnnualReports.IS_WEB: '否',
                    AnnualReports.IS_OUT_GUARANTEE: '否',
                    AnnualReports.IS_TRANSFER: '否',
                    AnnualReports.BUSINESS_ACTIVITIES: data.get('mainBusiAct'),
                }
                base_info_dict.update(sub_model)

        return base_info_dict

    # 年报网站信息
    def get_annual_report_websites_info(self, websites_info):
        websites_info_dict = {}
        websites_info_list = []
        websites_info_dict[AnnualReports.WEBSITES_INFO] = websites_info_list
        pages_list = self.get_crawl_page(websites_info, True)
        if not pages_list:
            return websites_info_dict
        for page in pages_list:
            json_data = util.json_loads(page.get('text'))
            if not json_data:
                continue
            data_list = json_data.get('data')
            if not data_list:
                continue

            for data in data_list:
                sub_model = {
                    AnnualReports.WebSitesInfo.NAME: data.get('webSitName'),
                    AnnualReports.WebSitesInfo.TYPE: '网站' if data.get('webType') == '1' else '网店',
                    AnnualReports.WebSitesInfo.SITE: data.get('domain'),
                }
                websites_info_list.append(sub_model)

        if len(websites_info_list) > 0:
            websites_info_dict[AnnualReports.IS_WEB] = '是'

        return websites_info_dict

    # 年报股东及出资信息
    def get_annual_report_shareholder_info(self, shareholder_info):
        shareholder_info_dict = {}
        shareholder_info_list = []
        shareholder_info_dict[AnnualReports.SHAREHOLDER_INFO] = shareholder_info_list
        pages_list = self.get_crawl_page(shareholder_info, True)
        if not pages_list:
            return shareholder_info_dict
        for page in pages_list:
            json_data = util.json_loads(page.get('text'))
            if not json_data:
                continue
            data_list = json_data.get('data')
            if not data_list:
                continue

            for data in data_list:
                sub_model = {
                    AnnualReports.ShareholderInfo.SHAREHOLDER_NAME: data.get('invName'),
                    AnnualReports.ShareholderInfo.SUBSCRIPTION_AMOUNT: util.get_amount_with_unit(data.get('liSubConAm')),
                    AnnualReports.ShareholderInfo.SUBSCRIPTION_TIME: util.convert_time(data.get('subConDate')),
                    AnnualReports.ShareholderInfo.SUBSCRIPTION_TYPE: data.get('subConFormName'),
                    AnnualReports.ShareholderInfo.PAIED_AMOUNT: util.get_amount_with_unit(data.get('liAcConAm')),
                    AnnualReports.ShareholderInfo.PAIED_TIME: util.convert_time(data.get('acConDate')),
                    AnnualReports.ShareholderInfo.PAIED_TYPE: data.get('acConForm_CN'),
                }
                shareholder_info_list.append(sub_model)

        return shareholder_info_dict

    # 年报对外投资信息
    def get_annual_report_invested_companies_info(self, invested_companies_info):
        invested_companies_info_dict = {}
        invested_companies_info_list = []
        invested_companies_info_dict[AnnualReports.INVESTED_COMPANIES_INFO] = invested_companies_info_list
        pages_list = self.get_crawl_page(invested_companies_info, True)
        if not pages_list:
            return invested_companies_info_dict
        for page in pages_list:
            json_data = util.json_loads(page.get('text'))
            if not json_data:
                continue
            data_list = json_data.get('data')
            if not data_list:
                continue

            for data in data_list:
                sub_model = {
                    AnnualReports.InvestedCompaniesInfo.COMPANY_NAME: data.get('entName'),
                    AnnualReports.InvestedCompaniesInfo.CODE: data.get('uniscId'),
                }
                invested_companies_info_list.append(sub_model)
        if len(invested_companies_info_list) > 0:
            invested_companies_info_dict[AnnualReports.IS_INVEST] = '是'

        return invested_companies_info_dict

    # 年报企业资产状况信息
    def get_annual_report_enterprise_asset_status_info(self, enterprise_asset_status_info):
        enterprise_asset_status_info_dict = {}
        enterprise_asset_status_info_list = []
        enterprise_asset_status_info_dict[AnnualReports.ENTERPRISE_ASSET_STATUS_INFO] = enterprise_asset_status_info_list
        pages_list = self.get_crawl_page(enterprise_asset_status_info, True)
        if not pages_list:
            return enterprise_asset_status_info_dict
        for page in pages_list:
            json_data = util.json_loads(page.get('text'))
            if not json_data:
                continue
            data_list = json_data.get('data')
            if not data_list:
                continue

            for data in data_list:
                sub_model = {
                    AnnualReports.EnterpriseAssetStatusInfo.GENERAL_ASSETS: data.get('assGro') if data.get('assGroDisIs') == '1' else '企业选择不公示',
                    AnnualReports.EnterpriseAssetStatusInfo.TOTAL_EQUITY: data.get('totEqu') if data.get('totEquDisIs') == '1' else '企业选择不公示',
                    AnnualReports.EnterpriseAssetStatusInfo.GROSS_SALES: data.get('vendInc') if data.get('vendIncDisIs') == '1' else '企业选择不公示',
                    AnnualReports.EnterpriseAssetStatusInfo.TOTAL_PROFIT: data.get('proGro') if data.get('proGroDisIs') == '1' else '企业选择不公示',
                    AnnualReports.EnterpriseAssetStatusInfo.INCOME_OF_TOTAL: data.get('maiBusInc') if data.get('maiBusIncDisIs') == '1' else '企业选择不公示',
                    AnnualReports.EnterpriseAssetStatusInfo.RETAINED_PROFITS: data.get('netInc') if data.get('netIncDisIs') == '1' else '企业选择不公示',
                    AnnualReports.EnterpriseAssetStatusInfo.TOTAL_TAX: data.get('ratGro') if data.get('ratGroDisIs') == '1' else '企业选择不公示',
                    AnnualReports.EnterpriseAssetStatusInfo.TOTAL_INDEBTEDNESS: data.get('liaGro') if data.get('liaGroDisIs') == '1' else '企业选择不公示',
                }
                enterprise_asset_status_info_list.append(sub_model)

        return enterprise_asset_status_info_dict

    # 年报对外提供担保保证信息
    def get_annual_report_out_guarantee_info(self, out_guarantee_info):
        out_guarantee_info_dict = {}
        out_guarantee_info_list = []
        out_guarantee_info_dict[
            AnnualReports.OUT_GUARANTEE_INFO] = out_guarantee_info_list
        pages_list = self.get_crawl_page(out_guarantee_info, True)
        if not pages_list:
            return out_guarantee_info_dict
        for page in pages_list:
            json_data = util.json_loads(page.get('text'))
            if not json_data:
                continue
            data_list = json_data.get('data')
            if not data_list:
                continue

            for data in data_list:
                sub_model = {
                    AnnualReports.OutGuaranteeInfo.CREDITOR: data.get('more'),
                    AnnualReports.OutGuaranteeInfo.OBLIGOR: data.get('mortgagor'),
                    AnnualReports.OutGuaranteeInfo.DEBT_TYPE: '合同' if data.get('priClaSecKind') == '1' or '合同' else '其他',
                    AnnualReports.OutGuaranteeInfo.DEBT_AMOUNT: data.get('priClaSecAm'),
                    AnnualReports.OutGuaranteeInfo.PERFORMANCE_PERIOD: '{}-{}'.format(
                        util.convert_time(data.get('pefPerForm')), util.convert_time(data.get('pefPerTo'))),
                    AnnualReports.OutGuaranteeInfo.GUARANTEE_PERIOD: data.get('guaranperiod'),
                    AnnualReports.OutGuaranteeInfo.GUARANTEE_TYPE: data.get('gaType'),

                }
                out_guarantee_info_list.append(sub_model)

        if len(out_guarantee_info_list) > 0:
            out_guarantee_info_dict[AnnualReports.IS_OUT_GUARANTEE] = '是'

        return out_guarantee_info_dict

    # 年报股权变更信息
    def get_annual_report_edit_shareholding_change_info(self, edit_shareholding_change_info):
        edit_shareholding_change_info_dict = {}
        edit_shareholding_change_info_list = []
        edit_shareholding_change_info_dict[
            AnnualReports.EDIT_SHAREHOLDING_CHANGE_INFO] = edit_shareholding_change_info_list
        pages_list = self.get_crawl_page(edit_shareholding_change_info, True)
        if not pages_list:
            return edit_shareholding_change_info_dict
        for page in pages_list:
            json_data = util.json_loads(page.get('text'))
            if not json_data:
                continue
            data_list = json_data.get('data')
            if not data_list:
                continue

            for data in data_list:
                sub_model = {
                    AnnualReports.EditShareholdingChangeInfo.SHAREHOLDER_NAME: data.get('inv'),
                    AnnualReports.EditShareholdingChangeInfo.BEFORE_CONTENT: '{}%'.format(
                        data.get('transAmPr')) if '%' not in str(data.get('transAmPr')) else data.get('transAmPr'),
                    AnnualReports.EditShareholdingChangeInfo.AFTER_CONTENT: '{}%'.format(
                        data.get('transAmAft')) if '%' not in str(data.get('transAmAft')) else data.get('transAmAft'),
                    AnnualReports.EditShareholdingChangeInfo.CHANGE_DATE: util.convert_time(data.get('altDate')),
                }
                edit_shareholding_change_info_list.append(sub_model)

        if len(edit_shareholding_change_info_list) > 0:
            edit_shareholding_change_info_dict[AnnualReports.IS_TRANSFER] = '是'

        return edit_shareholding_change_info_dict

    # 年报社保信息
    def get_annual_report_social_security_info(self, social_security_info):
        social_security_info_dict = {}
        social_security_info_list = []
        social_security_info_dict[
            AnnualReports.SOCIAL_SECURITY_INFO] = social_security_info_list
        pages_list = self.get_crawl_page(social_security_info, True)
        if not pages_list:
            return social_security_info_dict
        for page in pages_list:
            json_data = util.json_loads(page.get('text'))
            if not json_data:
                continue
            data_list = json_data.get('data')
            if not data_list:
                continue

            for data in data_list:
                sub_model = {
                    AnnualReports.SocialSecurityInfo.ENDOWMENT_INSURANCE: '{}人'.format(data.get('so110')),
                    AnnualReports.SocialSecurityInfo.UNEMPLOYMENT_INSURANCE: '{}人'.format(data.get('so210')),
                    AnnualReports.SocialSecurityInfo.MEDICAL_INSURANCE: '{}人'.format(data.get('so310')),
                    AnnualReports.SocialSecurityInfo.EMPLOYMENT_INJURY_INSURANCE: '{}人'.format(data.get('so410')),
                    AnnualReports.SocialSecurityInfo.BIRTH_INSURANCE: '{}人'.format(data.get('so510')),

                    AnnualReports.SocialSecurityInfo.EXPENDS_BASE_OF_ENDOWMENT_INSURANCE: '{}万元'.format(
                        data.get('totalWagesSo110')) if data.get('totalWagesDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.EXPENDS_BASE_OF_UNEMPLOYMENT_INSURANCE: '{}万元'.format(
                        data.get('totalWagesSo210')) if data.get('totalWagesDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.EXPENDS_BASE_OF_MEDICAL_INSURANCE: '{}万元'.format(
                        data.get('totalWagesSo310')) if data.get('totalWagesDis') == '1' else '企业选择不公示',
                    # 单位参加工伤保险缴费基数在官网前端被注释掉了
                    # AnnualReports.SocialSecurityInfo.EXPENDS_BASE_OF_EMPLOYMENT_INJURY_INSURANCE: '{}万元'.format(
                    #     data.get('totalWagesSo410')) if data.get('totalWagesDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.EXPENDS_BASE_OF_BIRTH_INSURANCE: '{}万元'.format(
                        data.get('totalWagesSo510')) if data.get('totalWagesDis') == '1' else '企业选择不公示',

                    AnnualReports.SocialSecurityInfo.ACTUAL_EXPENDS_BASE_OF_ENDOWMENT_INSURANCE: '{}万元'.format(
                        data.get('totalPaymentSo110')) if data.get('totalPaymentDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.ACTUAL_EXPENDS_BASE_OF_UNEMPLOYMENT_INSURANCE: '{}万元'.format(
                        data.get('totalPaymentSo210')) if data.get('totalPaymentDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.ACTUAL_EXPENDS_BASE_OF_MEDICAL_INSURANCE: '{}万元'.format(
                        data.get('totalPaymentSo310')) if data.get('totalPaymentDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.ACTUAL_EXPENDS_BASE_OF_EMPLOYMENT_INJURY_INSURANCE: '{}万元'.format(
                        data.get('totalPaymentSo410')) if data.get('totalPaymentDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.ACTUAL_EXPENDS_BASE_OF_BIRTH_INSURANCE: '{}万元'.format(
                        data.get('totalPaymentSo510')) if data.get('totalPaymentDis') == '1' else '企业选择不公示',

                    AnnualReports.SocialSecurityInfo.OUTSTANDING_AMOUNT_OF_ENDOWMENT_INSURANCE: '{}万元'.format(
                        data.get('unpaidSocialInsSo110')) if data.get('unpaidSocialInsDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.OUTSTANDING_AMOUNT_OF_UNEMPLOYMENT_INSURANCE: '{}万元'.format(
                        data.get('unpaidSocialInsSo210')) if data.get('unpaidSocialInsDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.OUTSTANDING_AMOUNT_OF_MEDICAL_INSURANCE: '{}万元'.format(
                        data.get('unpaidSocialInsSo310')) if data.get('unpaidSocialInsDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.OUTSTANDING_AMOUNT_OF_EMPLOYMENT_INJURY_INSURANCE: '{}万元'.format(
                        data.get('unpaidSocialInsSo410')) if data.get('unpaidSocialInsDis') == '1' else '企业选择不公示',
                    AnnualReports.SocialSecurityInfo.OUTSTANDING_AMOUNT_OF_BIRTH_INSURANCE: '{}万元'.format(
                        data.get('unpaidSocialInsSo510')) if data.get('unpaidSocialInsDis') == '1' else '企业选择不公示',
                }
                social_security_info_list.append(sub_model)

        return social_security_info_dict

    # 年报修改信息
    def get_annual_report_edit_change_info(self, edit_change_info):
        edit_change_info_dict = {}
        edit_change_info_list = []
        edit_change_info_dict[
            AnnualReports.EDIT_CHANGE_INFO] = edit_change_info_list
        pages_list = self.get_crawl_page(edit_change_info, True)
        if not pages_list:
            return edit_change_info_dict
        for page in pages_list:
            json_data = util.json_loads(page.get('text'))
            if not json_data:
                continue
            data_list = json_data.get('data')
            if not data_list:
                continue

            for data in data_list:
                sub_model = {
                    AnnualReports.EditChangeInfo.CHANGE_ITEM: data.get('alitem'),
                    AnnualReports.EditChangeInfo.BEFORE_CONTENT: data.get('altBe'),
                    AnnualReports.EditChangeInfo.AFTER_CONTENT: data.get('altAf'),
                    AnnualReports.EditChangeInfo.CHANGE_DATE: util.convert_time(data.get('altDate')),

                }
                edit_change_info_list.append(sub_model)

        return edit_change_info_dict
